﻿using System;
using System.IO;
using BSDS.Service.Configuration;

namespace BSDS.Service.Logic
{
    public class FilesManager
    {
        private readonly FilesPergingOptions pergingOptions;

        public DirectoryInfo InputDirectory { get; private set; }
        public DirectoryInfo FailedDirectory { get; private set; }
        public DirectoryInfo SuccessDirectory { get; private set; }
        public DirectoryInfo GeocodedDirectory { get; private set; }
        public DirectoryInfo ProcessingDirectory { get; private set; }
        public DirectoryInfo DownloadedDirectory { get; private set; }

        public FilesManager(DirectoryInfo inputDirectory, FilesPergingOptions pergingOptions)
        {
            this.pergingOptions = pergingOptions;
            InputDirectory = inputDirectory;
        }

        public void Initialize()
        {
            string successFolderName = Path.Combine(InputDirectory.FullName, "success");
            string failedFolderName = Path.Combine(InputDirectory.FullName, "failed");
            string geocodedFolderName = Path.Combine(InputDirectory.FullName, "geocoded");
            string processingFolderName = Path.Combine(InputDirectory.FullName, "processing");
            string downloadedFolderName = Path.Combine(InputDirectory.FullName, "downloaded");

            SuccessDirectory = new DirectoryInfo(successFolderName);
            FailedDirectory = new DirectoryInfo(failedFolderName);
            GeocodedDirectory = new DirectoryInfo(geocodedFolderName);
            ProcessingDirectory = new DirectoryInfo(processingFolderName);
            DownloadedDirectory = new DirectoryInfo(downloadedFolderName);

            if (!SuccessDirectory.Exists)
            {
                SuccessDirectory.Create();
            }

            if (!FailedDirectory.Exists)
            {
                FailedDirectory.Create();
            }

            if (!GeocodedDirectory.Exists)
            {
                GeocodedDirectory.Create();
            }

            if (!ProcessingDirectory.Exists)
            {
                ProcessingDirectory.Create();
            }

            if (!DownloadedDirectory.Exists)
            {
                DownloadedDirectory.Create();
            }

            Directory.SetCurrentDirectory(ProcessingDirectory.FullName);
        }

        public void MoveFileToFolder(string filePath, DirectoryInfo outputDirectory)
        {
            var file = new FileInfo(filePath);

            string outputFolderPath = Path.Combine(InputDirectory.FullName, outputDirectory.Name);
            string outputFilePath = Path.Combine(outputFolderPath, AppendDate(file.Name));

            file.MoveTo(outputFilePath);

        }

        private static string AppendDate(string filename)
        {
            return Path.GetFileNameWithoutExtension(filename) +
                   DateTime.Now.ToString("_yyyyMMddHHmmss") +
                   Path.GetExtension(filename);
        }

        public void CleanUpFiles()
        {
            CleanUpDirectory(SuccessDirectory, pergingOptions.SuccessFilesPergingInDays);
            CleanUpDirectory(FailedDirectory, pergingOptions.FailedFilesPergingInDays);
            CleanUpDirectory(GeocodedDirectory, pergingOptions.GeocodedFilesPergingInDays);
        }

        private void CleanUpDirectory(DirectoryInfo directory, int pruiningInDays)
        {
            if (pruiningInDays > 0)
            {
                foreach (var fileInfo in directory.GetFiles())
                {
                    if (DateTime.Now.Subtract(fileInfo.CreationTime).Days >= pruiningInDays)
                    {
                        fileInfo.Delete();
                    }
                }
            }
        }
    }
}